<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item v-if="false" label="出生年月" prop="birthDate">
        <el-date-picker clearable
          v-model="queryParams.birthDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出生年月">
        </el-date-picker>
      </el-form-item>-->
      <el-form-item label-width="90px" label="驾驶员姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入驾驶员姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item v-if="false" label="车队编号" prop="teamId">
        <el-input
          v-model="queryParams.teamId"
          placeholder="请输入车队编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label-width="70px" label="性别 " prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable>
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label-width="90px" label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label-width="100px" label="驾驶员身份号" prop="icCard">
        <el-input
          v-model="queryParams.icCard"
          placeholder="请输入驾驶员身份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label-width="100px" label="驾驶员类型" prop="driverType">
        <el-select v-model="queryParams.driverType" placeholder="请选择驾驶员类型" clearable>
          <el-option
            v-for="dict in dict.type.driver_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>-->
      <el-form-item label-width="90px" label="准驾车型" prop="drivingLicenseType">
        <el-select v-model="queryParams.drivingLicenseType" placeholder="请选择驾驶员类型" clearable>
          <el-option
            v-for="dict in dict.type.driving_license_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['driver:driverinfo:add']"
        >添加</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['driver:driverinfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete"
          v-hasPermi="['driver:driverinfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['driver:driverinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      border
      fit
      highlight-current-row
      empty-text="暂无数据"
      v-loading="loading"
      :data="driverinfoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" fixed="left" />
      <el-table-column v-if="false" label="主键" align="center" prop="id" />
      <el-table-column label="驾驶员姓名" width="100" align="center" prop="name" fixed="left" />
      <el-table-column label="性别" align="center" prop="sex" width="50">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column label="出生年月" align="center" prop="birthDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="车队名称" align="center" prop="teamId" width="200" /> -->
      <el-table-column label="运输公司" align="center" prop="transportationCompanyName" width="200" />
      
      <el-table-column label="联系电话" align="center" prop="phone" width="120" />
      <el-table-column label="驾驶员身份" align="center" prop="icCard" width="180" />
      <!--      <el-table-column label="身份证照片(最多2张)-附件" align="center" prop="idCardPhoto" />-->
      <!--      <el-table-column label="人脸照片(最多8张)-附件" align="center" prop="facePhoto" />-->
      <!--      <el-table-column label="健康证明(最多2张)-附件" align="center" prop="healthProof" />-->
      <!-- <el-table-column label="驾驶员类型" align="center" prop="driverType">
       
      </el-table-column>-->
      <el-table-column label="准驾车型" align="center" prop="drivingLicenseType" width="120">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.driving_license_type"
            :value="scope.row.drivingLicenseType"
          />
        </template>
      </el-table-column>
      <el-table-column label="初次领证日期" align="center" prop="firstIssueDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.firstIssueDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="驾驶证有限期开始" align="center" prop="licenseValidFrom" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.licenseValidFrom, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="驾驶证有限期结束" align="center" prop="licenseValidTo" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.licenseValidTo, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column  label="驾驶证档案编号" label-width="150px" align="center" prop="licenseFileNumber" /> -->
      <!-- <el-table-column label="驾驶证照片" align="center" prop="licensePhoto" /> -->
      <!--      <el-table-column label="从业资格信息-附件" align="center" prop="qualificationInfo" />-->
      <!--      <el-table-column label="机动车驾驶员安全信用表-附件" align="center" prop="driverSafetyCreditTable" />-->
      <!--      <el-table-column label="扩展位" align="center" prop="exts" />-->
      <!--      <el-table-column label="数据状态" align="center" prop="status" />-->
      <!-- <el-table-column label="是否创建登录账号" align="center" prop="userId" >
      </el-table-column>-->

      <el-table-column
        label="登录账号是否创建"
        align="center"
        prop="createSystemUser"
        width="180"
        fixed="right"
      >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.create_system_user" :value="scope.row.createSystemUser" />
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="220"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['driver:driverinfo:edit']"
          >修改</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleaddDriverUser(scope.row)"
            v-hasPermi="['driver:system:add']"
          >驾驶员账号</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['driver:driverinfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改驾驶员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px" class="fillUpForm">
        <el-row>
          <el-col :span="8">
            <el-form-item label="驾驶员姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入驾驶员姓名" />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="性别" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择性别">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="出生年月" prop="birthDate">
              <el-date-picker
                clearable
                v-model="form.birthDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择出生年月"
              ></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="驾驶员身份证" prop="icCard">
              <el-input v-model="form.icCard" placeholder="请输入驾驶员身份证" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="运输公司" prop="transportationCompanyId">

              <el-select v-model="form.transportationCompanyId" filterable placeholder="请输入运输公司" @change="changTransportationCompany">
                <el-option
                  v-for="item in transportationCompanyList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
            </el-select>

              <!-- <el-input v-model="form.transportationCompanyId" placeholder="请输入运输公司" /> -->
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
            <el-form-item label="车队名称" prop="teamId">
              <el-input v-model="form.teamId" placeholder="请输入车队名称" />
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-row>
          <!-- 
          <el-col :span="8">
            <el-form-item label="身份证照片" label-width="100px" prop="idCardPhoto">
              <image-upload v-model="form.idCardPhoto" />
            </el-form-item>
          </el-col>-->

          <!-- <el-col :span="8">
            <el-form-item label="人脸照片" prop="facePhoto">
              <image-upload v-model="form.facePhoto" />
            </el-form-item>
          </el-col>-->
          <!-- <el-col :span="8">
            <el-form-item label="健康证明" prop="healthProof">
              <image-upload v-model="form.healthProof" />
            </el-form-item>
          </el-col>-->
          <!-- <el-col :span="6">
        <el-form-item label="驾驶员类型" label-width="100px" prop="driverType">
          <el-select v-model="form.driverType" placeholder="请选择驾驶员类型">
            <el-option
              v-for="dict in dict.type.driver_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>-->

          <el-col :span="8">
            <el-form-item label="准驾车型" prop="drivingLicenseType">
              <!-- <el-input v-model="form.drivingLicenseType" placeholder="请输入准驾车型" /> -->
              <el-select v-model="form.drivingLicenseType" placeholder="请选择准驾车型">
                <el-option
                  v-for="dict in dict.type.driving_license_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="初次领证日期" prop="firstIssueDate">
              <el-date-picker
                clearable
                v-model="form.firstIssueDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择初次领证日期"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="驾驶证有限期开始" prop="licenseValidFrom">
              <el-date-picker
                clearable
                v-model="form.licenseValidFrom"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择驾驶证有限期开始"
              ></el-date-picker>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="驾驶证有限期结束" prop="licenseValidTo">
              <el-date-picker
                clearable
                v-model="form.licenseValidTo"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择驾驶证有限期结束"
              ></el-date-picker>
            </el-form-item>
          </el-col>

          <!-- <el-col :span="8">
            <el-form-item label="驾驶证档案编号" label-width="120px" prop="licenseFileNumber">
              <el-input v-model="form.licenseFileNumber" placeholder="请输入驾驶证档案编号" />
            </el-form-item>
          </el-col>-->
          <!-- 
          <el-col :span="8">
            <el-form-item label="驾驶证照片" label-width="100px" prop="licensePhoto">
              <image-upload v-model="form.licensePhoto" />
            </el-form-item>
          </el-col>-->

          <!-- <el-col :span="8">
            <el-form-item label="从业资格信息-附件" label-width="150px" prop="qualificationInfo">
              <image-upload v-model="form.qualificationInfo" />
            </el-form-item>
          </el-col>-->

          <!-- <el-col :span="8">
            <el-form-item label="机动车驾驶员安全信用表-附件" label-width="200px" prop="driverSafetyCreditTable">
              <image-upload v-model="form.driverSafetyCreditTable" />
            </el-form-item>
          </el-col>-->
          <!-- 
          <el-col :span="8">
            <el-form-item label="关联用户账号userId" label-width="200px" prop="userId">
              <el-input v-model="form.userId" placeholder="请输入关联用户账号userId" />
            </el-form-item>
          </el-col>-->
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDriverinfo,
  getDriverinfo,
  delDriverinfo,
  addDriverinfo,
  updateDriverinfo,
  addDriverUser
} from "@/api/driver/driverinfo";
import { listTransportationCompany} from "@/api/modules/transportationCompany";
export default {
  name: "Driverinfo",
  dicts: [
    "sys_user_sex",
    "driver_type",
    "create_system_user",
    "driving_license_type"
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 驾驶员信息表格数据
      driverinfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        birthDate: null,
        name: null,
        teamId: null,
        sex: null,
        phone: null,
        icCard: null,
        idCardPhoto: null,
        facePhoto: null,
        healthProof: null,
        driverType: null,
        drivingLicenseType: null,
        firstIssueDate: null,
        licenseValidFrom: null,
        licenseValidTo: null,
        licenseFileNumber: null,
        licensePhoto: null,
        qualificationInfo: null,
        driverSafetyCreditTable: null,
        exts: null,
        status: null,
        userId: null,
        createSystemUser: null,
        transportationCompanyId:null
      },
      // 运输公司
      transportationCompanyList:[],
       // 运输公司查询参数
       transportationCompanyQueryParams: {
        pageNum: 1,
        pageSize: 100
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "驾驶员姓名不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
        phone: [
          { required: true, message: "联系电话不能为空", trigger: "change" }
        ],
        birthDate: [
          { required: true, message: "出生年月不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getTransportationCompanyList();
  },
  methods: {
    /** 查询驾驶员信息列表 */
    getList() {
      this.loading = true;
      listDriverinfo(this.queryParams).then(response => {
        this.driverinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        birthDate: null,
        name: null,
        teamId: null,
        sex: null,
        phone: null,
        icCard: null,
        idCardPhoto: null,
        facePhoto: null,
        healthProof: null,
        driverType: null,
        drivingLicenseType: null,
        firstIssueDate: null,
        licenseValidFrom: null,
        licenseValidTo: null,
        licenseFileNumber: null,
        licensePhoto: null,
        qualificationInfo: null,
        driverSafetyCreditTable: null,
        exts: null,
        status: null,
        createTime: null,
        updateTime: null,
        userId: null,
        transportationCompanyId:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加驾驶员信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getDriverinfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改驾驶员信息";
      });
    },
    /** 创建司机系统账号 */
    handleaddDriverUser(row) {
      console.log("row===", row);
      const fromData = { id: row.id, phone: row.phone };
      console.log("fromData=", fromData);

      addDriverUser(fromData).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.resetQuery();
        this.open = false;
      });
    },

     /** 运输公司 */
     getTransportationCompanyList() {
      // this.loading = true;
      listTransportationCompany(this.transportationCompanyQueryParams).then(response => {
        this.transportationCompanyList = response.rows;
        console.log("===========",response.rows)
        // this.total = response.total;
        // this.loading = false;
      });
    },
    // 下拉选择运输公司
    changTransportationCompany(){
      this.getTransportationCompanyList();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDriverinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDriverinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除驾驶员信息编号为"' + ids + '"的数据项？')
        .then(function() {
          return delDriverinfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "driver/driverinfo/export",
        {
          ...this.queryParams
        },
        `driverinfo_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
<style lang="scss" scoped>
  
  .fillUpForm {
    .el-input{
      width: 100%;
    }
    .el-select{
      width: 100%;
    }
  }

</style>

